package radio.vnmedia.vn;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

public abstract class WifiListener {

		public abstract void WifiConnected();

		public abstract void WifiDisconnected();

		public BroadcastReceiver registerWifiStateChangeListener(Activity act) {

			BroadcastReceiver mWifiStateChangeListener = new BroadcastReceiver() {

				@Override
				public void onReceive(Context arg0, Intent intent) {
					if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
						NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
						if (networkInfo.isConnected()) {
							WifiConnected();
						}
					} else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
						NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
						if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI && !networkInfo.isConnected()) {
							WifiDisconnected();
						}
					}
				}
			};
			IntentFilter filter = new IntentFilter();
			filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
			filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
			act.registerReceiver(mWifiStateChangeListener, filter);
			return mWifiStateChangeListener;
		}

		public void unRegisterWifiStateChangeListener(Activity act, BroadcastReceiver mWifiStateChangeListener) {
			act.unregisterReceiver(mWifiStateChangeListener);
		}
	}